package edu.map.aamas.rnn;

import java.io.File;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import edu.map.aamas.gui.PondPanel;
import edu.map.aamas.gui.RnnEvolutionDisplay;
import edu.map.aamas.logging.RNNLogger;

public class BatchExperience implements RnnListener {
	private long numberOfExperiences = 2000;
	private RNNLogger logger = null;
	private int exp = 1;
	private String dir = "logs/batch";
	private JDialog frame;
	public BatchExperience(long numberOfExperiences, String dir) {
		
		File d = new File(".", dir);
		d.mkdirs();
		this.numberOfExperiences = numberOfExperiences;
		logger = new RNNLogger();
		RNN.getInstance().loggingEnabled = true;
		RNN.getInstance().useRandomSeed = false;
		rnnUpdated(RNN.getInstance());
		RnnEvolutionDisplay evo = new RnnEvolutionDisplay();
		RNN.getInstance().addRnrListener(this);
		RNN.getInstance().addRnrListener(logger);
		RNN.getInstance().addRnrListener(PondPanel.getInstance());
		RNN.getInstance().addRnrListener(evo);
		
		RNN.getInstance().currentController = "Random";							
		logger.forceFilename(dir, "random_controller.txt");		
		RNN.getInstance().millisBetweenIterations = 10;
		RNN.getInstance().init();
		RNN.getInstance().resume();
		
		frame = new JDialog();		
		frame.setSize(500, 400);
		JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		split.add(PondPanel.getInstance());
		split.add(evo);
		frame.getContentPane().add(split);
		frame.setModal(true);
		frame.setTitle("Saving logs to "+dir);
		frame.setVisible(true);
		
	}
	
	public void rnnRestarted(RNN rnr) {
		
	}
	
	public void rnnUpdated(RNN rnr) {

		if (rnr.iteration >= numberOfExperiences) {
			RNN.getInstance().pause();
			switch(exp) {
			case(0):
				RNN.getInstance().currentController = "Random";							
				logger.forceFilename(dir, "random_controller.txt");				
				RNN.getInstance().init();
				RNN.getInstance().resume();
				
				exp++;
				break;
				
			case(1):
				RNN.getInstance().currentController = "Harmonics";
				logger.forceFilename(dir, "harmonics_controller.txt");				
				RNN.getInstance().init();
				RNN.getInstance().resume();
				exp++;
				break;
			case (2):
				//RNN.getInstance().currentController = "RNR";
				//logger.forceFilename(dir, "RNN_Simple.txt");
				//RNN.getInstance().homeostasis = true;
				//RNN.getInstance().proprioception = false;
				//RNN.getInstance().plasticity = false;
				//RNN.getInstance().init();
				//RNN.getInstance().resume();
				exp++;
				//break;
			case (3):
				RNN.getInstance().currentController = "RNR";
				logger.forceFilename(dir, "RNN_Homeostasis.txt");
				RNN.getInstance().homeostasis = true;
				RNN.getInstance().proprioception = false;
				RNN.getInstance().plasticity = false;
				RNN.getInstance().init();
				RNN.getInstance().resume();
				exp++;
				break;
			case (4):
				RNN.getInstance().currentController = "RNR";
				logger.forceFilename(dir, "RNN_Proprioception.txt");
				RNN.getInstance().homeostasis = true;
				RNN.getInstance().proprioception = true;
				RNN.getInstance().plasticity = false;
				RNN.getInstance().init();
				RNN.getInstance().resume();
				exp++;
				break;
			case (5):
				RNN.getInstance().currentController = "RNR";
				logger.forceFilename(dir, "RNN_NeuralPlasticity.txt");
				RNN.getInstance().homeostasis = true;
				RNN.getInstance().proprioception = true;
				RNN.getInstance().plasticity = true;
				RNN.getInstance().init();
				RNN.getInstance().resume();
				exp++;
				break;	
			default:				
				frame.setVisible(false);
				frame.dispose();
				System.out.println("All experiences saved to "+dir);
				
			}
		}
	}
	
	public static void main(String[] args) {
		for (int i = 0; i < 100; i++)
			new BatchExperience(1000, "logs/batch"+i);
	}
}
